namespace Sharpmark.Tools.ProjectStyler
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;

    public class FileEncoder
    {
        public Encoding TargetEncoding { get; set; }
        public string[] ExtendFileNames { get; set; }

        public FileEncoder(Encoding coder)
        {
            TargetEncoding = coder;
            ExtendFileNames = new string[0];
        }
        public void EncodingToUTF8(string srcfile, string destfile)
        {
            var logwriter = new StreamWriter("logfile.txt", true, Encoding.UTF8);

            EncodingFile(srcfile, destfile);

            logwriter.WriteLine(srcfile);

            logwriter.Flush();
            logwriter.Close();
        }

        private void EncodingFile(string srcfile, string destfile)
        {
            FileStream input;
            StreamReader reader;
            FileStream output;

            input = new FileStream(srcfile, FileMode.Open);
            var encoding = GetEncoding(input);
            reader = new StreamReader(input, encoding);


            var filedata = reader.ReadToEnd();
            var encodingBytes = TargetEncoding.GetBytes(filedata);

            if (!Directory.Exists(Path.GetDirectoryName(destfile)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(destfile));
            }

            output = new FileStream(destfile, FileMode.Create);

            output.Write(encodingBytes, 0, encodingBytes.Length);
            output.Flush();
            output.Close();
        }

        public List<string> GetFormattedFiles()
        {
            var logreader = new StreamReader("logfile.txt");
            List<string> formattedFiles = new List<string>();
            while (!logreader.EndOfStream)
            {
                formattedFiles.Add(logreader.ReadLine());
            }
            logreader.Close();
            return formattedFiles;
        }
        public List<string> GetNewFilesToBeFormat(string directory, List<string> oldfiles)
        {
            List<string> results = new List<string>();
            if (ExtendFileNames.Length == 0) return results;

            foreach (var searchPattern in ExtendFileNames)
            {
                foreach (var srcfile in
                    Directory.GetFiles(directory, "*." + searchPattern, SearchOption.AllDirectories))
                {
                    if (!oldfiles.Contains(srcfile)) results.Add(srcfile);
                }
            }

            return results;
        }
        private static Encoding GetEncoding(Stream stream)
        {
            var targetEncoding = Encoding.Default;
            if (stream != null && stream.Length >= 2)
            {
                //保存文件流的前4个字节  
                byte byte1 = 0;
                byte byte2 = 0;
                byte byte3 = 0;
                byte byte4 = 0;
                //保存当前Seek位置  
                var origPos = stream.Seek(0, SeekOrigin.Begin);
                stream.Seek(0, SeekOrigin.Begin);

                var nByte = stream.ReadByte();
                byte1 = Convert.ToByte(nByte);
                byte2 = Convert.ToByte(stream.ReadByte());
                if (stream.Length >= 3)
                {
                    byte3 = Convert.ToByte(stream.ReadByte());
                }
                if (stream.Length >= 4)
                {
                    byte4 = Convert.ToByte(stream.ReadByte());
                }
                //根据文件流的前4个字节判断Encoding  
                //Unicode   {0xFF,   0xFE};  
                //BE-Unicode   {0xFE,   0xFF};  
                //UTF8   =   {0xEF,   0xBB,   0xBF};  
                if (byte1 == 0xFE && byte2 == 0xFF) //UnicodeBe  
                {
                    targetEncoding = Encoding.BigEndianUnicode;
                }
                if (byte1 == 0xFF && byte2 == 0xFE && byte3 != 0xFF) //Unicode  
                {
                    targetEncoding = Encoding.Unicode;
                }
                if (byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF) //UTF8  
                {
                    targetEncoding = Encoding.UTF8;
                }
                //恢复Seek位置                
                stream.Seek(origPos, SeekOrigin.Begin);
            }
            return targetEncoding;
        }
    }
}
